package com.wjh.graduation.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wjh.graduation.domain.Groups;
import com.wjh.graduation.domain.People;
import com.wjh.graduation.domain.R;
import com.wjh.graduation.domain.PieChart;
import com.wjh.graduation.service.GroupsService;
import com.wjh.graduation.service.PeopleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Wu Jiahong
 * @since 2022-01-25
 */
@RestController
@RequestMapping("/people")
@Api(tags = "人脉相关接口")
public class PeopleController {

	@Autowired
	PeopleService peopleService;

	@Autowired
	GroupsService groupsService;

	@ApiOperation("根据分组id获得人脉信息")
	@GetMapping("/byGroup")
	public R getPeople(@ApiParam("传入分组id，获得人脉数据") @RequestParam Integer id) {
		QueryWrapper<People> qw = new QueryWrapper<>();
		qw.select("people_id", "name").eq("group_id", id);
		List<People> list = peopleService.list(qw);
		return new R(200, list, "获取成功");
	}

	@ApiOperation("根据id获得人脉信息详情")
	@GetMapping("/getPeopleD")
	public R<Map<Object, Object>> getPeopleDetail(@ApiParam("人脉ID") @RequestParam Integer id) {
		//设置返回的map
		Map<Object, Object> map = new HashMap<>();
		People people = peopleService.getById(id);
		if (people == null) {
			return new R<Map<Object, Object>>(303, null, "获取数据失败");
		} else {
			//设置分组名称
			if (people.getGroupId() != 0) {
				Groups groups = groupsService.getById(people.getGroupId());
				if (groups != null) {
					map.put("groupName", groups.getName());
				}
			}
			map.put("detail", people);

			return new R<Map<Object, Object>>(200, map, "获取成功");
		}
	}

	@ApiOperation("新增人脉")
	@PostMapping("/create")
	public R create(@Validated @ApiParam("人脉字段,必须传入用户ID") @RequestBody People people) {
		if (people.getGroupId() == null) {
			//没分组id默认放到默认分组
			QueryWrapper<Groups> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("user_id", people.getUserId()).eq("name", "默认分组");
			Groups groups = groupsService.getOne(queryWrapper);
			if (groups != null) {
				people.setGroupId(groups.getGroupId());
				//设置好id就可以保存
				if (peopleService.save(people)) {
					return new R(200, "新增成功");
				} else {
					return new R(303, "新增失败");
				}
			} else {
				//查询分组出错
				throw new NullPointerException();
			}
		} else {
			//有分组id，要确保id存在
			if (groupsService.getById(people.getGroupId()) != null) {
				//存在进行保存
				if (peopleService.save(people)) {
					return new R(200, "新增成功");
				} else {
					return new R(303, "新增失败");
				}
			} else {
				//传入id不存在，抛出异常
				throw new NullPointerException();
			}
		}
	}

	@ApiOperation("删除人脉")
	@GetMapping("/delete")
	public R delete(@ApiParam("传入人脉的的ID") @RequestParam Integer id) {
		if (peopleService.removeById(id)) {
			return new R(200, "删除成功");
		} else {
			return new R(303, "删除失败");
		}
	}

	@ApiOperation("修改人脉信息")
	@PostMapping("/update")
	public R update(@Validated @ApiParam("人脉字段,必须传入用户ID和人脉ID") @RequestBody People people) {
		if (people.getPeopleId() == null) {
			//没有传入人脉id，抛出异常
			throw new NullPointerException();
		}
		//类似新增
		if (people.getGroupId() == null) {
			//没分组id默认放到默认分组
			QueryWrapper<Groups> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("user_id", people.getUserId()).eq("name", "默认分组");
			Groups groups = groupsService.getOne(queryWrapper);
			if (groups != null) {
				people.setUserId(groups.getUserId());
				//设置好id就可以保存
				if (peopleService.updateById(people)) {
					return new R(200, "新增成功");
				} else {
					return new R(303, "新增失败");
				}
			} else {
				//查询分组出错
				throw new NullPointerException();
			}
		} else {
			//有分组id，要确保id存在
			if (groupsService.getById(people.getGroupId()) != null) {
				//存在进行保存
				if (peopleService.updateById(people)) {
					return new R(200, "新增成功");
				} else {
					return new R(303, "新增失败");
				}
			} else {
				//传入id不存在，抛出异常
				throw new NullPointerException();
			}
		}
	}

	@ApiOperation("性别分析")
	@GetMapping("/sexAnalysis")
	public R sexAnalysis(@ApiParam("用户Id") @RequestParam Integer userId) {
		QueryWrapper<People> queryWrapperM = new QueryWrapper<>();
		QueryWrapper<People> queryWrapperW = new QueryWrapper<>();
		//建立不同的查询条件
		queryWrapperM.eq("user_id", userId).eq("sex", "1");
		queryWrapperW.eq("user_id", userId).eq("sex", "0");
		//查询数据
		int countM = peopleService.count(queryWrapperM);
		int countW = peopleService.count(queryWrapperW);
		List<PieChart> list = new ArrayList<PieChart>();
		if (countM > 0) {
			list.add(new PieChart("男", (double) countM));
		}
		if (countW > 0) {
			list.add(new PieChart("女", (double) countW));
		}
		return new R(200, list, "获取成功");
	}

	@ApiOperation("年龄分析")
	@GetMapping("/ageAnalysis")
	public R<List<PieChart>> ageAnalysis(@ApiParam("用户Id") @RequestParam Integer userId) {
		QueryWrapper<People> queryWrapper122 = new QueryWrapper<>();
		QueryWrapper<People> queryWrapper2335 = new QueryWrapper<>();
		QueryWrapper<People> queryWrapper3650 = new QueryWrapper<>();
		QueryWrapper<People> queryWrapper50120 = new QueryWrapper<>();
		//建立不同的查询条件
		queryWrapper122.eq("user_id", userId).apply("TIMESTAMPDIFF(YEAR,people.birthday,CURDATE()) BETWEEN '1' and '22'");
		queryWrapper2335.eq("user_id", userId).apply("TIMESTAMPDIFF(YEAR,people.birthday,CURDATE()) BETWEEN '23' and '35'");
		queryWrapper3650.eq("user_id", userId).apply("TIMESTAMPDIFF(YEAR,people.birthday,CURDATE()) BETWEEN '36' and '50'");
		queryWrapper50120.eq("user_id", userId).apply("TIMESTAMPDIFF(YEAR,people.birthday,CURDATE()) BETWEEN '51' and '200'");
		//查询数据
		int count1 = peopleService.count(queryWrapper122);
		int count2 = peopleService.count(queryWrapper2335);
		int count3 = peopleService.count(queryWrapper3650);
		int count5 = peopleService.count(queryWrapper50120);
		List<PieChart> list = new ArrayList<PieChart>();
		if (count1 > 0) {
			list.add(new PieChart("1-22岁", (double) count1));
		}
		if (count2 > 0) {
			list.add(new PieChart("23-35岁", (double) count2));
		}
		if (count3 > 0) {
			list.add(new PieChart("26-50岁", (double) count3));
		}
		if (count5 > 0) {
			list.add(new PieChart("50岁以上", (double) count5));
		}

		return new R(200, list, "获取成功");
	}
}
